﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.waitingQueue {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.controllers.data.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.world.*;

    public class QueuedPetBubbleMediator extends Mediator {

        private const DURATION_TYPE_SMALL:int = 240;
        private const DURATION_TYPE_MEDIUM:int = 720;
        private const DURATION_TF_OFFSET_X:int = 300;
        private const CONTAINER_OFFSET_Y:int = 140;
        private const NEEDS_ICONS_OFFSET_Y:int = 115;
        private const NEEDS_ICONS_OFFSET_X:int = 5;
        private const REWARD_CONTAINER_OFFSET_Y:int = 410;
        private const REWARD_CONTAINER_OFFSET_X:int = 270;
        private const REWARD_ICON_TF_OFFSET_X:int = 120;
        private const REWARD_ICON_TF_OFFSET_Y:int = 10;
        private const REWARD_OFFSET_X:int = 90;
        private const ICON_PADDING_Y:int = 75;
        private const GEM_CONTAINER_OFFSET_Y:int = 120;
        private const GEM_CONTAINER_OFFSET_X:int = 230;

        public var computerModel:ComputerModel;
        public var languageModel:LanguageModel;
        public var worldModel:WorldModel;
        public var sessionModel:SessionModel;
        private var patientPoolVo:PatientPoolVO;
        private var durationType:String;
        private var currentPet:WaitingPetVO;
        private var rollOutTimer:Timer;
        private var story:MovieClip;

        override public function onRegister():void{
            super.onRegister();
            this.view.hide();
            eventMap.mapListener(eventDispatcher, EventConstants.CLICK_ON_WAITING_PET, this.onPetClick, InfoEvent);
            eventMap.mapListener(eventDispatcher, EventConstants.DOUBLE_CLICKED_WAITING_PET, this.onDoubleClicked, InfoEvent);
            eventMap.mapListener(eventDispatcher, EventConstants.MOUSE_OUT_ON_WAITING_PET, this.onPetMouseOut, InfoEvent);
        }
        private function onPetClick(_arg1:InfoEvent):void{
            this.destroyTimer();
            this.removeStory();
            this.view.clear();
            this.view.show();
            this.view.addEventListener(MouseEvent.ROLL_OUT, this.onRollOut);
            this.view.addEventListener(MouseEvent.ROLL_OVER, this.onRollOver);
            this.currentPet = _arg1.info.vo;
            this.updatePosition((_arg1.info.position as Point));
            this.getPatienPoolVo();
            this.setDuration();
            this.parseDurationType();
            this.setFilter();
            this.setNeededRooms();
            this.setRewards();
            this.initHealButton();
            this.initMouseOutTimer();
            this.setStory();
        }
        private function onPetMouseOut(_arg1:InfoEvent):void{
            if (((this.currentPet) && ((this.currentPet.id == _arg1.id)))){
                this.onRollOut();
            };
        }
        private function onDoubleClicked(_arg1:InfoEvent):void{
            this.currentPet = (_arg1.info as WaitingPetVO);
            this.onHealClick();
        }
        private function onRollOut(_arg1:Event=null):void{
            this.rollOutTimer.start();
        }
        private function onRollOver(_arg1:Event):void{
            this.rollOutTimer.reset();
        }
        private function initMouseOutTimer():void{
            this.rollOutTimer = new Timer(300, 1);
            this.rollOutTimer.addEventListener(TimerEvent.TIMER, this.onTimer);
        }
        private function onTimer(_arg1:Event):void{
            this.destroyBubble();
        }
        private function destroyTimer():void{
            if (this.rollOutTimer){
                this.rollOutTimer.removeEventListener(TimerEvent.TIMER, this.onTimer);
                this.rollOutTimer = null;
            };
        }
        private function destroyBubble():void{
            this.removeStory();
            this.destroyTimer();
            this.computerModel.lastClickedPetId = "";
            var _local1:MovieClip = this.view.background.healButton;
            _local1.removeEventListener(MouseEvent.CLICK, this.onHealClick);
            _local1.removeEventListener(MouseEvent.MOUSE_OVER, this.onHealOver);
            _local1.removeEventListener(MouseEvent.MOUSE_OUT, this.onHealOut);
            this.view.removeEventListener(MouseEvent.ROLL_OUT, this.onRollOut);
            this.view.removeEventListener(MouseEvent.ROLL_OVER, this.onRollOver);
            this.view.hide();
            this.patientPoolVo = null;
            this.currentPet = null;
        }
        private function updatePosition(_arg1:Point):void{
            this.view.x = (_arg1.x + SpeechBubbleConstants.WAITING_QUEUE_OFFSET_LEFT);
            this.view.y = (_arg1.y + SpeechBubbleConstants.WAITING_QUEUE_OFFSET_TOP);
        }
        private function getPatienPoolVo():void{
            this.patientPoolVo = this.computerModel.getPatienPoolVoByDiseaseName(this.currentPet.type);
        }
        private function setNeededRooms():void{
            var _local2:MovieClip;
            var _local5:String;
            var _local1:DynamicTF = new DynamicTF("0x4A3764", 52);
            _local1.htmlText = this.languageModel.getTextByKey("global_needs");
            _local1.y = this.CONTAINER_OFFSET_Y;
            _local1.x = 0;
            this.view.content.addChild(_local1);
            var _local3:MovieClip = new MovieClip();
            _local3.x = this.NEEDS_ICONS_OFFSET_X;
            var _local4:int = (this.CONTAINER_OFFSET_Y + this.ICON_PADDING_Y);
            for each (_local5 in this.patientPoolVo.treatments) {
                _local2 = AssetsLibrary.getMovieClip(("Logo" + _local5));
                _local2.scaleX = (_local2.scaleY = 0.9);
                _local2.y = _local4;
                _local4 = (_local4 + this.NEEDS_ICONS_OFFSET_Y);
                _local3.addChild(_local2);
            };
            this.view.content.addChild(_local3);
        }
        private function setRewards():void{
            var _local1:Sprite = new Sprite();
            _local1.x = this.REWARD_CONTAINER_OFFSET_X;
            _local1.y = this.CONTAINER_OFFSET_Y;
            var _local2:DynamicTF = new DynamicTF("0x4A3764", 52);
            _local2.htmlText = this.languageModel.getTextByKey("global_reward");
            _local2.y = 0;
            _local2.x = 0;
            _local1.addChild(_local2);
            this.view.content.addChild(_local1);
            this.addReward(_local1, "CoinIcon", this.patientPoolVo.coins.toString(), (this.REWARD_OFFSET_X - 10));
            this.addReward(_local1, "CoinXP", this.patientPoolVo.xp.toString(), (this.REWARD_OFFSET_X * 2));
            this.addGem(_local1);
        }
        private function addReward(_arg1:Sprite, _arg2:String, _arg3:String, _arg4:int):void{
            var _local5:Sprite = new Sprite();
            _local5.y = _arg4;
            var _local6:MovieClip = AssetsLibrary.getMovieClip(_arg2);
            AssetsLibrary.getMovieClip(_arg2).scaleX = (_local6.scaleY = 5);
            _local5.addChild(_local6);
            var _local7:DynamicTF = new DynamicTF("0x4A3764", 52);
            _local7.htmlText = _arg3;
            _local7.x = this.REWARD_ICON_TF_OFFSET_X;
            _local7.y = this.REWARD_ICON_TF_OFFSET_Y;
            _local5.addChild(_local7);
            _arg1.addChild(_local5);
        }
        private function addGem(_arg1:Sprite):void{
            var _local2:Sprite;
            var _local3:String;
            var _local4:MovieClip;
            var _local5:DynamicTF;
            if (((this.hasFountain()) && ((this.patientPoolVo.rewardIcon.length >= 2)))){
                _local2 = new Sprite();
                _local2.y = this.GEM_CONTAINER_OFFSET_Y;
                _local2.x = this.GEM_CONTAINER_OFFSET_X;
                _local3 = (("Reward" + Strings.capitalize(this.patientPoolVo.rewardIcon)) + "Thumb");
                _local4 = AssetsLibrary.getMovieClip(_local3);
                _local4.filters = [Filters.getFilter(Filters.WHITE_GLOW)];
                _local4.scaleX = (_local4.scaleY = 2);
                _local2.addChild(_local4);
                _local5 = new DynamicTF("0x4A3764", 62);
                _local5.htmlText = this.patientPoolVo.numberOfRewards.toString();
                _local5.x = (_local5.x + (((_local4.width / 2) - _local5.width) - 10));
                _local5.y = (_local5.y + ((_local4.height / 2) - (_local5.height / 2)));
                _local2.addChild(_local5);
                _arg1.addChild(_local2);
            };
        }
        private function initHealButton():void{
            var _local1:MovieClip = this.view.background.healButton;
            _local1.gotoAndStop("default");
            _local1.addEventListener(MouseEvent.CLICK, this.onHealClick);
            _local1.addEventListener(MouseEvent.MOUSE_OVER, this.onHealOver);
            _local1.addEventListener(MouseEvent.MOUSE_OUT, this.onHealOut);
            _local1.buttonMode = true;
            _local1.useHandCursor = true;
            this.setHealButtonLabel();
        }
        private function setHealButtonLabel():void{
            var _local1:DynamicTF = new DynamicTF("0xFFFFFF", 20);
            _local1.htmlText = this.languageModel.getTextByKey("global_button_select");
            _local1.x = 0;
            _local1.y = 0;
            _local1.width = 75;
            this.view.background.healButton.addChild(_local1);
        }
        private function onHealOut(_arg1:MouseEvent):void{
            var _local2:MovieClip = (_arg1.target as MovieClip);
            _local2.gotoAndStop("default");
        }
        private function onHealOver(_arg1:MouseEvent):void{
            var _local2:MovieClip = (_arg1.target as MovieClip);
            _local2.gotoAndStop("hover");
        }
        private function onHealClick(_arg1:MouseEvent=null):void{
            dispatch(new InfoEvent(EventConstants.WALK_TO_RECEPTION, this.currentPet.id));
            this.destroyBubble();
        }
        private function setDuration():void{
            var _local1:DynamicTF = new DynamicTF("0xFFFFFF", 72, null, "ShowcardGothic");
            _local1.x = this.DURATION_TF_OFFSET_X;
            var _local2:String = TimeUtils.getComputerDisplayDurationFormat(this.patientPoolVo.duration, this.languageModel.getTextByKey("reception_selectionbox_roomminutes"), this.languageModel.getTextByKey("reception_selectionbox_roomhours"), this.languageModel.getTextByKey("reception_selectionbox_roomhours_sing"), this.languageModel.getTextByKey("reception_selectionbox_roomdays"), this.languageModel.getTextByKey("reception_selectionbox_roomday"));
            _local1.htmlText = _local2;
            this.view.content.addChild(_local1);
        }
        private function parseDurationType():void{
            if (this.patientPoolVo.duration <= this.DURATION_TYPE_SMALL){
                this.durationType = "green";
            } else {
                if ((((this.patientPoolVo.duration > this.DURATION_TYPE_SMALL)) && ((this.patientPoolVo.duration <= this.DURATION_TYPE_MEDIUM)))){
                    this.durationType = "orange";
                } else {
                    this.durationType = "red";
                };
            };
            this.view.background.duration.gotoAndStop(this.durationType);
        }
        private function setStory():void{
            if (this.showStory()){
                this.story = AssetsLibrary.getMovieClip((SpeechBubbleConstants.STORY + this.currentPet.type));
                if (this.story){
                    this.view.setStory(this.story);
                    this.story.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
                };
            };
        }
        private function showStory():Boolean{
            var _local1:int;
            for each (_local1 in this.sessionModel.viewedDiseaseStorys) {
                if (this.currentPet.diseaseId == _local1){
                    return (false);
                };
            };
            return (true);
        }
        private function onEnterFrame(_arg1:Event):void{
            if (this.story.currentFrame >= this.story.framesLoaded){
                this.removeStory();
            };
        }
        private function removeStory():void{
            if (this.story){
                this.story.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
                this.view.removeStory();
            };
            if (this.currentPet){
                this.sessionModel.viewedDiseaseStorys.push(this.currentPet.diseaseId);
            };
        }
        private function setFilter():void{
            this.view.filters = [Filters.getFilter(("QueueBubble" + this.durationType))];
        }
        private function hasFountain():Boolean{
            return (this.worldModel.hasAreaType(GameConstants.TYPE_FOUNTAIN));
        }
        private function get view():QueuedPetBubble{
            return ((viewComponent as QueuedPetBubble));
        }

    }
}//package net.wooga.pets.views.waitingQueue 
